/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buisness;

import dal.UsersDAL;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Users;

/**
 *
 * @author gabriellebotbol
 */
public class Administration extends HttpServlet 
{
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        response.sendRedirect("./administration.jsp");
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        //change mdp change visib
        //change mot de passe
        Users user=(Users) request.getSession().getAttribute("username");
        if (user!=null)
        {
            String oldPassword=request.getParameter("oldPassword");
            String newPassword=request.getParameter("newPassword");
            String visibility=request.getParameter("visibility");

            if(oldPassword!=null && newPassword!=null && visibility!=null)
            {
                if(oldPassword.equals(user.getPassword()))
                {
                    Users newUser=new Users(user.getIdFriend(),
                            user.getUsername(),
                            newPassword,
                            user.getFirstname(),
                            user.getLastname(),
                            user.getBirthdate(),
                            user.getMail(),
                            user.getPhone(),
                            user.getCity(),
                            user.getCountry(),
                            user.getAvatar(),
                            visibility);
                    UsersDAL.updateUser(newUser);
                    //rajouter user dans la session:
                    request.getSession().setAttribute("username", newUser);
                    response.sendRedirect("./news.jsp");
                }
                else
                {
                    request.getSession().setAttribute("wrong_password", true);
                    response.sendRedirect("./administration.jsp");
                }
            }
        }
        else
        {
            response.sendRedirect("./login.jsp");
        }
    }
}
